define(["require",
        "dojo/_base/declare", 
        "gistech/widgets/baseWidget/BaseWidget",
        "dojo/_base/lang",
        "dojo/on",
        "dojo/html",
        "dojo/topic", 
        "esri/Color",      
        "esri/SpatialReference",
        "esri/toolbars/draw",
        "esri/symbols/SimpleFillSymbol",  
        "esri/symbols/PictureMarkerSymbol",
        "esri/graphic",
        "esri/tasks/QueryTask",
        "esri/tasks/query",
        "gistech/widgets/QueryInfoWindow/QueryInfoWindow",
        "esri/geometry/Point",
        "esri/graphic",
        "dojo/text!./templates/Template.html"],
        function(require,declare,BaseWidget,lang,on,html,topic,Color,SpatialReference,Draw,
        		SimpleFillSymbol,PictureMarkerSymbol, Graphic,QueryTask,Query,QueryInfoWindow,Point,Graphic,
        		template) {
	return declare([BaseWidget], {
		//模板页面
		templateString : template,
		
		map:null,
		handles:[],
		//点符号,用于显示初始传入坐标的地图位置
		AddrSymbol:null,
		drawtools:null,
		symbol:null,
		params:null,
		constructor : function(obj,node) {
			this.inherited(arguments);
			this.params = obj;
			this.map = this.params.map;	
			this.gOtherLayer = this.map.getLayer("other");
			this.gVehicleLayer = this.map.getLayer("vehicleLayer");
			this.gPeopleLayer = this.map.getLayer("peopleLayer");
			this.drawtools = new Draw(this.map);
			this.symbol = new SimpleFillSymbol().setColor(new Color([255, 0, 0, 0.25]));
		},
		//组件初始化方法
		postCreate : function() {
			this.inherited(arguments);
			//注册组件是添加的监听事件列表，关闭时移除监听，以避免重复
			handles = [on(this.drawtools,"draw-end", lang.hitch(this,this.addFeatureToMap))];//图层选中状态变化时，更新卷帘对比图层列表
			AddrSymbol = new esri.symbol.PictureMarkerSymbol(require.toUrl("./images/Location.png"), 45,40);
		},
		destroy:function(){

			this.inherited(arguments);
			//this.map = null;
		},
		startup:function(){
			this.inherited(arguments);
			dojo.query(".drawing").map(lang.hitch(this,function(item,index){  
				dojo.connect(item,"onclick",lang.hitch(this,function(){  
					this.activateTool({id:item.id});
				}));  
			}));
		},
		activateTool:function(itemobj) {
			this.drawtools.activate(itemobj.id);
		},
		addFeatureToMap:function (evt) {
			this.removeFeatureFromMap();//画之前先清空
			this.drawtools.deactivate();
			var graphic = new Graphic(evt.geometry, this.symbol);
			graphic.bType = "ExtendQuery";
			this.gOtherLayer.add(graphic);
			this.queryListByGeometry(evt.geometry);
		},
		
		//删除feature
		removeFeatureFromMap:function(){
			if (this.queryInfoWindow!=null) {
       		     this.queryInfoWindow.destroy();
       		     this.queryInfoWindow=null;
       	    }            	
			for (var i = this.gOtherLayer.graphics.length - 1; i >= 0; i--) {
                var temGraphic =this.gOtherLayer.graphics[i];
                if (temGraphic != null && temGraphic != undefined && temGraphic.bType == "ExtendQuery") {
                	if (Global.SelectGraphic==temGraphic){
                    	//this.map.infoWindow.hide();
                    	Global.SelectGraphic=null;
                    }
                	this.gOtherLayer.remove(temGraphic);
                }
            }
		},
		
		queryListByGeometry:function (geometry){
			//var QueryTask = new esri.tasks.QueryTask(this.params.url);//"http://192.168.120.69:6080/arcgis/rest/services/POC/kehu/MapServer/0");
			//var query = new esri.tasks.Query();
			//query.geometry = geometry; 
			var peopleGraphic = [];
			if(Global.StreamLayer){
				for (var i = Global.StreamLayer.graphics.length - 1; i >= 0; i--) {
		            var temGraphic = Global.StreamLayer.graphics[i];
		            if (temGraphic != null && temGraphic != undefined && geometry.contains(temGraphic.geometry)) {
		            	
		            	peopleGraphic.push(temGraphic);
		            }
		        }
			}
			if (Global.clusterLayer){
				for (var i = Global.clusterLayer.graphics.length - 1; i >= 0; i--) {
					 var temGraphic = Global.clusterLayer.graphics[i];
					 if (temGraphic != null && temGraphic != undefined && temGraphic.bType && temGraphic.bType=="C" && geometry.contains(temGraphic.geometry)) {
						 if (Global.clusterLayer._singles.length>0 && Global.clusterLayer._singles[0].attributes.clusterId==temGraphic.attributes.clusterId){
							 continue;
						 }

						 for ( var j = 0, il = Global.clusterLayer._clusterData.length; j < il; j++) {
			    	        if (temGraphic.attributes.clusterId == Global.clusterLayer._clusterData[j].attributes.clusterId ) {
			    	        	
			    	        	var g = new Graphic(
			    	      	          new Point(Global.clusterLayer._clusterData[j].x, Global.clusterLayer._clusterData[j].y, Global.clusterLayer._sr),
			    	      	          null,
			    	      	          Global.clusterLayer._clusterData[j].attributes
			    	      	    );
			    	        	peopleGraphic.push(g);
			    	        }
			    	    }
					 }
				}
			}
			
			var vehicleGraphic = [];
			if(this.gVehicleLayer){
				for (var i = this.gVehicleLayer.graphics.length - 1; i >= 0; i--) {
	                var temGraphic = this.gVehicleLayer.graphics[i];
	                if (temGraphic != null && temGraphic != undefined && temGraphic.bType == "real" && geometry.contains(temGraphic.geometry)) {
	                	if (Global.SelectGraphic==temGraphic){
	                    	//this.map.infoWindow.hide();
	                    	Global.SelectGraphic=null;
	                    }
	                	vehicleGraphic.push(temGraphic);
	                }
	            }
			}
			
			this.map.infoWindow.hide();//先隐藏其他infowindow
			if (this.queryInfoWindow!=null) {
       		     this.queryInfoWindow.destroy();
       		     this.queryInfoWindow=null;
       	    }            	
            this.queryInfoWindow = new QueryInfoWindow({
		       	 container:dojo.body(),
		       	 vehicleGraphic:vehicleGraphic,
		       	 peopleGraphic:peopleGraphic,
       	    });
            this.queryInfoWindow.startup();
            Global.QueryInfoWindow = this.queryInfoWindow;
            Global.contentInfoWin = this.queryInfoWindow;
            //var screenPt = this.map.toScreen(graArray[index+firstnum-1].geometry);
            this.map.infoWindow.resize(230, 300);
           	this.map.infoWindow.setTitle("统计结果");
           	
           	var anchorPt,anchor;
           	if(geometry.rings){//框选形状为圆时
           		anchorPt = new Point((geometry.cache._extent.xmax+geometry.cache._extent.xmin)/2,geometry.cache._extent.ymax,this.map.spatialReference);
           	}else{
           		anchorPt = new Point((geometry.xmax+geometry.xmin)/2,geometry.ymax,this.map.spatialReference);
           	}
            anchor = new Graphic(anchorPt);
            this.map.infoWindow.anchor="top";
            this.map.infoWindow.setContent(this.queryInfoWindow.domNode);
            this.map.infoWindow.show(anchor.geometry,this.map.getInfoWindowAnchor(anchor.geometry) );
            //this.map.infoWindow.show(query.geometry, this.map.getInfoWindowAnchor(query.geometry));
        	//domClass.add(this.map.infoWindow.domNode, "real");
            //mapUtil.zoomToGraphics(this.map,array);
			
			//console.log(vehicleGraphic);
		},

	});
});
